﻿Imports System.Text.RegularExpressions
Imports System.Net
Imports System.Collections.Specialized

Public Class UpdateChecker
	Public Event UpdateStatus(NewVersion As String)
	Public Event UpdateLangauges(AvailableLanguages As List(Of KeyValuePair(Of String, Translation)))

	Const latestUrl As String = "http://nwtcompiler.codeplex.com/SourceControl/BrowseLatest"
	Const versionFile As String = "#NWTCompiler/My Project/AssemblyInfo.vb"

	Public Sub CheckVersion()
		Try
			Using client As New System.Net.WebClient
				Dim Page As String = client.DownloadString("http://nwtcompiler.codeplex.com/")

				Dim regexVersionMatch As New Regex("(?<=right_sidebar.*)[0-9]\.[0-9]\.[0-9]", RegexOptions.Singleline)
				Dim regexMatchResult As Match = regexVersionMatch.Match(Page)

				If regexMatchResult.Success Then
					IsNewerVersion(New Version(regexMatchResult.Value))
				End If

			End Using

			'Dim fileUrl As String

			'Dim request As WebRequest
			'request = WebRequest.Create(latestUrl)
			'request.Method = "GET"
			'Using response = request.GetResponse()

			'	Dim originalUrlRegex As String = "(?<base>http://nwtcompiler.codeplex.com/SourceControl/changeset/)(?<changeset>.*)$"
			'	Dim newUrlRegex As String = "${base}file/${changeset}"

			'	fileUrl = Regex.Replace(response.ResponseUri.AbsoluteUri, originalUrlRegex, newUrlRegex)
			'	fileUrl = response.ResponseUri.AbsoluteUri & versionFile
			'End Using

			'request = WebRequest.Create(fileUrl)
			'request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
			'request.Headers.Add("X-Requested-With", "XMLHttpRequest")
			'request.Method = "POST"

			'Dim content() As Byte = Text.Encoding.UTF8.GetBytes(versionFile)
			'request.ContentLength = content.Length

			'Using requestStream As IO.Stream = request.GetRequestStream
			'	requestStream.Write(content, 0, content.Length)
			'End Using

			'Using response = request.GetResponse
			'	Using streamReader As New IO.StreamReader(response.GetResponseStream)
			'		Dim result As String = streamReader.ReadToEnd.Trim
			'		Dim regexFindVersionNumber As String = "(?<=AssemblyFileVersion.*>&quot;)([\d/.]*)"
			'		Dim currentVersion As String = Regex.Match(result, regexFindVersionNumber).Value

			'		If currentVersion <> "" Then
			'			IsNewerVersion(New Version(currentVersion))
			'		End If
			'	End Using
			'End Using

		Catch ex As Exception
			'there is something wrong with the version checking; ignore it
		End Try

	End Sub

	Private Sub IsNewerVersion(WebVersion As Version)

		Select Case WebVersion.CompareTo(New Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly.Location).FileVersion.ToString))
			Case 0
				'the same
			Case 1
				'WebVersion is newer than the current version
				RaiseEvent UpdateStatus(WebVersion.ToString)
			Case -1
				'WebVersion is older than the current version
		End Select

	End Sub

	Public Sub CheckLanguages(LanguageCount As List(Of KeyValuePair(Of String, Translation)))
		Dim myLangauges As List(Of KeyValuePair(Of String, Translation)) = GetLanguages()

		For i = myLangauges.Count - 1 To 0 Step -1
			Dim linqi = i
			If LanguageCount.Any(Function(x) x.Value.LanguageCode = myLangauges(linqi).Value.LanguageCode) Then
				myLangauges.RemoveAt(i)
			End If
		Next

		myLangauges = GetLanguageData(myLangauges)

		For i = myLangauges.Count - 1 To 0 Step -1
			Dim linqi = i
			If LanguageCount.Any(Function(x) x.Value.LanguageCode = myLangauges(linqi).Value.LanguageCode) Then
				myLangauges.RemoveAt(i)
			End If
		Next

		If myLangauges.Count > 0 Then
			RaiseEvent UpdateLangauges(GetLanguageData(myLangauges))
		End If
	End Sub
End Class
